package com.ruoyi.livesass.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cyl.manager.lbs.domain.LbsZbjl;
import com.cyl.manager.lbs.service.LbsZbjlService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.livesass.api.sync.LiveActivityDataSynService;
import com.ruoyi.livesass.api.sync.LiveUserTimeDataToDaySynService;
import com.ruoyi.livesass.api.sync.LiveUserTimeDataToDaySynServiceGssj;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;



@RestController
@RequestMapping("/hs/callback")
public class HsCallBackController extends BaseController {
	
	public static void main(String...args) {
		System.out.println(System.currentTimeMillis() / 1000);
	}
	
	@Autowired
	LiveActivityDataSynService liveActivityDataSynService;
	
	@Autowired
	LiveUserTimeDataToDaySynService liveUserTimeDataToDaySynService;
	
	@Autowired
	LbsZbjlService lbsZbjlService;
	
	@Autowired
	LiveUserTimeDataToDaySynServiceGssj liveUserTimeDataToDaySynServiceGssj;
	
	
    @PostMapping("/activity")
    public ResponseEntity<String> activity(String ActivityID, String EventType, String Status, String Timestamp, 
			String Sign, String SubAccountName, String RequestUuid) {
      
    	
		System.out.println("收到请求.Sign["+Sign+"] || ActivityID=" + ActivityID +" | EventType=" 
		+ EventType + " | Status="+ Status + " | SubAccountName=" + SubAccountName + " | Timestamp=" + Timestamp + " | RequestUuid=" + RequestUuid);
		
    	
		if("ActivityStatusModifyCallBack".equals(EventType)) { //直播间状态回调
			
			if("1".equals(Status)) { //开始
				//liveUserTimeDataToDaySynService.activityStartPorcess();
				liveActivityDataSynService.startSync(ActivityID, SubAccountName);
				//1742566846
				//更新直播间真实开始时间
				
			}
			
			if("4".equals(Status)) { //结束
				try {
					liveUserTimeDataToDaySynService.activityStopPorcess(ActivityID);
				} catch (Exception e) {
					e.printStackTrace();
				}
				liveActivityDataSynService.stopSync(ActivityID, SubAccountName);
				
			}
			
			updateLbsZbjl(ActivityID, Timestamp, Status);
		}
		
    	
        return ResponseEntity.ok("完成");
    }
		
		
	private void updateLbsZbjl(String ActivityID, String Timestamp, String Status) {
		LbsZbjl lbsZbjl = lbsZbjlService.selectById(Long.valueOf(ActivityID));
		if(null != lbsZbjl) {
			if("1".equals(Status)) { //开始
				Long startTimestamp = Long.valueOf(Timestamp) * 1000;
				String zskssj = DateTime.of(startTimestamp).toString();
				if(StrUtil.isBlankIfStr(lbsZbjl.getZskssj())) {
					lbsZbjl.setZskssj(zskssj);
				}
				
				if(!StrUtil.isBlankIfStr(lbsZbjl.getZsjssj())) {
					lbsZbjl.setZsjssj(null);
				}
				
			}
			
			if("4".equals(Status)) { //结束
				Long endTimestamp = Long.valueOf(Timestamp) * 1000;
				String zsjssj = DateTime.of(endTimestamp).toString();
				if(StrUtil.isBlankIfStr(lbsZbjl.getZsjssj())) {
					lbsZbjl.setZsjssj(zsjssj);
				}else {
					String jhjssj = lbsZbjl.getJhjssj();
					if(!StrUtil.isBlankIfStr(jhjssj)) {
						DateTime jhjssjTime = DateTime.of(jhjssj, "yyyy-MM-dd hh:mm:ss");
						if(endTimestamp < jhjssjTime.getTime() ) {
							lbsZbjl.setZsjssj(zsjssj);
						}
					}
				}
			}
		}
		lbsZbjlService.update(lbsZbjl);
	}
	
	
	
	@PostMapping("/syncSjfw")
    public ResponseEntity<String> syncSjfw(Integer kssj, Integer jssj, String ack) throws Exception {
		if(null == ack || !"ydb20250327".equals(ack)) {
			return ResponseEntity.ok("完成");
		}
    	
		liveUserTimeDataToDaySynServiceGssj.performTask(kssj, jssj);
    	
        return ResponseEntity.ok("完成");
    }

}
